<template>
  <div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="false"
      label-width="68px"
    >
      <el-form-item label="vue配置一级id" prop="vueFirstId">
        <el-input
          v-model="queryParams.vueFirstId"
          placeholder="请输入vue配置一级id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组件路径" prop="componentPath">
        <el-input
          v-model="queryParams.componentPath"
          placeholder="请输入组件路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-show="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['conf:BiConfVueSecond:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['conf:BiConfVueSecond:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['conf:BiConfVueSecond:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['conf:BiConfVueSecond:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handlePosition"
          >修改位置</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="dragTable"
      row-key="id"
      :data="BiConfVueSecondList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        class-name="allowDrag"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        class-name="allowDrag"
        label="id"
        align="center"
        prop="id"
      />
      <el-table-column
        class-name="allowDrag"
        label="vue配置一级id"
        align="center"
        prop="vueFirstId"
      />
      <el-table-column
        class-name="allowDrag"
        label="组件路径"
        align="center"
        prop="componentPath"
      />
      <el-table-column
        class-name="allowDrag"
        label="位置排序"
        align="center"
        prop="positionOrder"
      /> -->
      <!-- <el-table-column label="标题信息" align="center" prop="titleInfo" /> -->
      <!-- <el-table-column label="表名" align="center" prop="tableName" /> -->
      <el-table-column
        class-name="allowDrag"
        label="组件路径"
        align="center"
        prop="componentPath"
      />
      <el-table-column label="配置x轴" align="center" prop="">
        <template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row, 5)"
            >配置x轴</span
          >
        </template></el-table-column
      >
      <el-table-column label="属性信息" align="center" prop="limit">
        <template slot-scope="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row, 10)"
            >属性信息</span
          >
        </template></el-table-column
      >
      <el-table-column label="vue布局信息" align="center" prop="limit">
        <template slot-scope="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row, 20)"
            >vue布局信息</span
          >
        </template></el-table-column
      >
      <el-table-column label="vue参数列表" align="center" prop="limit">
        <template slot-scope="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row, 30)"
            >vue参数列表</span
          >
        </template></el-table-column
      >
      <el-table-column
        label="二级id(参数)"
        align="center"
        prop="vueSecondIdS"
      ></el-table-column>
      <el-table-column label="sqlId" align="center" prop="sqlId">
        <template slot-scope="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row, 40)"
            >{{ scope.row.sqlId }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="触发列表" align="center" prop="sqlId">
        <template slot-scope="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row, 50)"
            >触发列表</span
          >
        </template>
      </el-table-column>
      <el-table-column label="是否触发器" align="center" prop="isTriggered">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isTriggered"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="id列表"
        align="center"
        prop="vueSecondIdList"
      ></el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['conf:BiConfVueSecond:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['conf:BiConfVueSecond:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改vue配置二级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="vue配置一级id" prop="vueFirstId">
          <el-input
            v-model="form.vueFirstId"
            placeholder="请输入vue配置一级id"
          />
        </el-form-item> -->
        <el-form-item label="组件路径" prop="componentPath">
          <!-- <el-input
            
            v-model="form.componentPath"
            placeholder="请输入组件路径"
          /> -->
          <el-select v-model="form.componentPath" clearable>
            <el-option
              v-for="item in componentPathList"
              :key="item"
              :label="`${item}`"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置排序" prop="positionOrder">
          <el-input
            v-model="form.positionOrder"
            placeholder="请输入位置排序"
            clearable
          />
        </el-form-item>
        <el-form-item label="标题信息" prop="titleInfo">
          <el-input
            v-model="form.titleInfo"
            placeholder="请输入标题信息"
            clearable
          />
        </el-form-item>
        <!-- <el-form-item label="表名" prop="tableName">
          <el-select
            v-model="form.tableName"
            clearable
            filterable
            remote
            :remote-method="getDatacomment"
            placeholder="请输入表名"
            allow-create
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.comment}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="sqlId" prop="sqlId">
          <el-input v-model="form.sqlId" placeholder="请输入sqlId" clearable />
        </el-form-item>
        <el-form-item label="二级id(参数)" prop="vueSecondIdS">
          <el-input
            v-model="form.vueSecondIdS"
            placeholder="请输入二级id(参数)"
            clearable
          />
        </el-form-item>
        <el-form-item label="id列表" prop="vueSecondIdList">
          <el-input
            v-model="form.vueSecondIdList"
            placeholder="请输入id列表"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog
      :visible.sync="openInfo"
      width="1500px"
      append-to-body
      v-if="openInfo"
    >
      <div class="el-dialog-div">
        <component :is="currentComponent" :row="row"></component>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="openSqlFirstField"
      width="1500px"
      append-to-body
      v-if="openSqlFirstField"
    >
      <div class="el-dialog-div">
        <component
          :is="currentComponent"
          :row="row"
          @handleSqlId="handleSqlId"
        ></component>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiConfVueSecond,
  getBiConfVueSecond,
  delBiConfVueSecond,
  addBiConfVueSecond,
  updateBiConfVueSecond,
  getTableNameByVueSecondId,
  adjustVueSecondPosition,
} from "@/api/vue/BiConfVueSecond";
import {
  selectTableFields,
  selectTableNameList,
} from "@/api/conf/SysTableField";
import { listBiSqlFromTable } from "@/api/conf/sql/BiSqlFromTable";
import BiVueAdditionInfo from "@/views/vue/BiVueAdditionInfo/index.vue";
import BiVuePropertyInfo from "@/views/vue/BiVuePropertyInfo/index.vue";
import BiVueLayoutInfo from "@/views/vue/BiVueLayoutInfo/index.vue";
import BiVueParamDwd from "@/views/vue/BiVueParamDwd/index.vue";
import BiVueTriggerDwd from "@/views/vue/BiVueTriggerDwd/index.vue";
import BiSqlFirstField from "@/views/conf/BiSqlFirstField/index.vue";
import Sortable from "sortablejs";
export default {
  name: "BiConfVueSecond",
  components: {
    BiVueAdditionInfo,
    BiVuePropertyInfo,
    BiVueLayoutInfo,
    BiVueParamDwd,
    BiSqlFirstField,
    BiVueTriggerDwd,
  },
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      openSqlFirstField: false,
      componentPathList: ["chartData", "After", "PanelGroup", "listAll", "day"],
      row: {},
      rowInfo: {},
      openInfo: false,
      tablecomment: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // vue配置二级表格数据
      BiConfVueSecondList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vueFirstId: null,
        componentPath: null,
        tableName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      panelType: 5,
    };
  },
  watch: {
    id: {
      handler: function (newnew, oldold) {
        console.log("newnew", newnew);
        if (newnew) {
          this.queryParams.id = newnew;
          // this.form.id=newnew

          this.getList();
          //   this.gettableNameList();
        }
      },
      deep: true,
      immediate: true,
    },
    // BiConfVueSecondList: {
    //   handler: function (newnew, oldold) {
    //     console.log("newnew", newnew);
    //     this.BiConfVueSecondList = newnew;
    //     this.handlePosition();
    //   },
    //   deep: true,
    //   immediate: true,
    // },
  },
  computed: {
    // ...mapGetters(["userId"]),
    currentComponent() {
      let type = parseInt(this.panelType);
      switch (type) {
        case 5:
          return "BiVueAdditionInfo";
        case 10:
          return "BiVuePropertyInfo";

        case 20:
          return "BiVueLayoutInfo";
        case 30:
          return "BiVueParamDwd";
        case 40:
          return "BiSqlFirstField";
        case 50:
          return "BiVueTriggerDwd";
        default:
          return "";
      }
    },
  },
  created() {
    // this.getList();
  },
  mounted() {
    const el = this.$refs.dragTable.$el.querySelectorAll(
      ".el-table__body-wrapper > table > tbody"
    )[0];
    const sortable = Sortable.create(el, {
      handle: ".allowDrag",
      onEnd: (evt) => {
        const targetRow = this.BiConfVueSecondList.splice(evt.oldIndex, 1)[0];
        this.BiConfVueSecondList.splice(evt.newIndex, 0, targetRow);
        for (let index in this.BiConfVueSecondList) {
          this.BiConfVueSecondList[index].sort = parseInt(index) + 1;
        }
      },
    });
    console.log(JSON.stringify(this.BiConfVueSecondList));
  },

  methods: {
    handleSqlId(id) {
      delete this.rowInfo.sqlId;
      this.form = { ...this.rowInfo, sqlId: id };
      updateBiConfVueSecond(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.openSqlFirstField = false;
        this.getList();
      });
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.isTriggered === "0" ? "触发" : "不触发";
      this.$modal
        .confirm('确认要"' + text + '"？')
        .then(function () {
          return updateBiConfVueSecond(row);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isTriggered = row.isTriggered === "0" ? "1" : "0";
        });
    },
    handleGo(path) {
      this.$router.push({
        path: path,
        // query: { tableName: "bi_pur_inv_monitor" },
      });
    },
    selectformisNew(row, panelType) {
      this.rowInfo = row;
      this.panelType = panelType;
      let count = 0;
      count++;
      if (this.panelType == 5 || this.panelType == 10) {
        getTableNameByVueSecondId({ vueSecondId: row.id }).then((response) => {
          if (!response) {
            this.$modal.msgError("需要表");
            return;
          }
          this.row = {
            count: count,
            id: row.id,
            tableName: response,
            sqlId: row.sqlId,
          };
        });
      } else {
        this.row = { count: count, id: row.id, sqlId: row.sqlId };
      }
      if (this.panelType == 40) {
        this.openSqlFirstField = true;
      } else {
        this.openInfo = true;
      }
    },
    getDatacomment(key) {
      selectTableNameList({ comment: key }).then((response) => {
        this.tablecomment = response;
      });
    },
    handlePosition() {
      adjustVueSecondPosition(this.BiConfVueSecondList).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess("修改位置成功");
        }
        this.getList();
      });
    },
    /** 查询vue配置二级列表 */
    getList() {
      //   this.loading = true;
      listBiConfVueSecond(this.queryParams).then((response) => {
        this.BiConfVueSecondList = response.rows;
        this.total = response.total;
        // this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vueFirstId: null,
        componentPath: null,
        tableName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (!this.id) {
        this.$modal.msgError("至少选择一条数据");
        return;
      }
      this.reset();
      this.open = true;
      this.title = "添加vue配置二级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiConfVueSecond(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改vue配置二级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.vueFirstId = this.id;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiConfVueSecond(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiConfVueSecond(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除vue配置二级编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiConfVueSecond(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "conf/BiConfVueSecond/export",
        {
          ...this.queryParams,
        },
        `BiConfVueSecond_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.el-dialog-div {
  height: 80vh;
  overflow-x: hidden;
}
